//
//  MyDetailModel.swift
//  股票框架
//
//  Created by 李小骥 on 2017/10/19.
//  Copyright © 2017年 李小骥. All rights reserved.
//

import UIKit
struct Birthday{
    var year:String
    var month:String
    var day:String
    init(birthDayString:String) {
        if (birthDayString.count == 8){
            let yearRange = birthDayString.startIndex...birthDayString.index(birthDayString.startIndex, offsetBy: 3)
            year = String(birthDayString[yearRange])
            let monthRange = birthDayString.index(birthDayString.startIndex, offsetBy: 4)...birthDayString.index(birthDayString.startIndex, offsetBy: 5)
            month = String(birthDayString[monthRange])
            let dayRange = birthDayString.index(birthDayString.startIndex, offsetBy: 6)..<birthDayString.endIndex
            day = String(birthDayString[dayRange])
        }
        else{
            year = ""
            month = ""
            day = ""
        }
    }
    func birthdayString() -> String{
        return year+month+day
    }
}
enum Gender:String {
    case male = "1"
    case female = "2"
    case unknown = "3"
}
class MyDetailModel: NSObject {
    var headImgUrl = String(){
        willSet{
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "image", value: newValue) { (result) in
                print("upload:image:\(newValue)")
            }
        }
    }
    var nickName = String(){
        willSet{
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "user_name", value: newValue) { (result) in
                print("upload:user_name:\(newValue)")
            }
        }
    }
    var gender = Gender(rawValue: "3"){
        willSet{
            print(newValue)
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "sex", value: (newValue?.rawValue)!) { (result) in
                print("upload:sex:\((newValue?.rawValue)!)")
            }
        }
    }
    var qq = String(){
        willSet{
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "qq", value: newValue) { (result) in
                print("upload:qq:\(newValue)")
            }
        }
    }
    var birthday = Birthday(birthDayString: ""){
        willSet{
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "birthday", value: newValue.birthdayString()) { (result) in
                print("upload:birthday:\(newValue)")
            }
        }
    }
    var area = String(){
        willSet{
            Utils.AlamofireAsynchronousUpdateUserInfo(key: "area", value: newValue) { (result) in
                print("upload:area:\(newValue)")
            }
        }
    }
    var headImg = UIImage(named:"unloginHead.png"){
        willSet{
            if let image = newValue{
            Utils.AlamofireAsynchronousUploadImage(headImage: image) { (result) in
                print("upload:headimage:\(String(describing: newValue))")
            }
            }
        }
    }
    override init() {
        if let lc = lCode{
            headImgUrl = lc.imageUrl
            nickName = lc.user_name
            gender = Gender(rawValue: lc.sex)
            qq = lc.qq
            birthday = Birthday(birthDayString: lc.birthday)
            area = lc.area
            headImg = currentHeadImage
    }
        super.init()
}
    func updateMyDetailModel(newNickName:String){
        nickName = newNickName
    }
    func updateMyDetailModel(newHeadImgUrl:String){
        headImgUrl = newHeadImgUrl
    }
    func updateMyDetailModel(newGender:String){
        gender = Gender(rawValue: newGender)
    }
    func updateMyDetailModel(newQQ:String){
        qq = newQQ
    }
    func updateMyDetailModel(newBirthday:String){
        birthday = Birthday(birthDayString: newBirthday)
    }
    func updateMyDetailModel(newArea:String){
        area = newArea
    }
    func updateMyDetailModel(newheadImg:UIImage){
        headImg = newheadImg
    }
}
